<?php

$bootstrapDirPower = is_writeable('../bootstrap');
$storageDirPower = is_writeable('../storage');
$uploadsDirPower = is_writeable('./uploads');

$powerData = [];
$powerData['bootstrap'] = $bootstrapDirPower;
$powerData['storage'] = $storageDirPower;
$powerData['public/uploads'] = $uploadsDirPower;

$envExists = file_exists('../.env');
$existsData = [];
$existsData['.env'] = $envExists;

function getDirSize($dir)
{
    $handle = opendir($dir);
    $sizeResult = 0;
    while (false !== ($FolderOrFile = readdir($handle))) {
        if ($FolderOrFile != "." && $FolderOrFile != "..") {
            if (is_dir("$dir/$FolderOrFile")) {
                $sizeResult += getDirSize("$dir/$FolderOrFile");
            } else {
                $sizeResult += filesize("$dir/$FolderOrFile");
            }
        }
    }
    closedir($handle);
    return $sizeResult;
}

$sizeData = [];
$sizeData['日志文件(storage/logs/)'] = getDirSize('../storage/logs');
$sizeData['页面缓存(storage/framework/views/)'] = getDirSize('../storage/framework/views');
$sizeData['数据缓存(storage/framework/cache/)'] = getDirSize('../storage/framework/cache/data');
$sizeData['上传文件(public/uploads/)'] = getDirSize('./uploads');


function checkdir($basedir)
{
    if ($dh = opendir($basedir)) {
        while (($file = readdir($dh)) !== false) {
            if ($file != '.' && $file != '..') {
                if (!is_dir($basedir . "/" . $file)) {
                    echo checkBOM("$basedir/$file");
                } else {
                    $dirname = $basedir . "/" . $file;
                    checkdir($dirname);
                }
            }
        }
        closedir($dh);
    }
}

function checkBOM($filename)
{
    global $auto, $bomFiles;
    $contents = file_get_contents($filename);
    $charset[1] = substr($contents, 0, 1);
    $charset[2] = substr($contents, 1, 1);
    $charset[3] = substr($contents, 2, 1);
    if (ord($charset[1]) == 239 && ord($charset[2]) == 187 && ord($charset[3]) == 191) {
        $bomFiles[] = $filename;
        if ($auto == 1) {
            $rest = substr($contents, 3);
            rewrite($filename, $rest);
        } else {
        }
    };
}

function rewrite($filename, $data)
{
    $filenum = fopen($filename, "w");
    flock($filenum, LOCK_EX);
    fwrite($filenum, $data);
    fclose($filenum);
}

$auto = 1;
$bomFiles = [];
checkdir('./');

?>
<style type="text/css">
    /** Table 表格样式 **/
    .qgg-table {
        border-collapse: collapse;
        width: 80%;
        border: 1px solid #c6c6c6 !important;
        margin-bottom: 20px;
    }

    .qgg-table th {
        border-collapse: collapse;
        border-right: 1px solid #c6c6c6 !important;
        border-bottom: 1px solid #c6c6c6 !important;
        background-color: #ddeeff !important;
        padding: 5px 9px;
        font-size: 14px;
        font-weight: normal;
        text-align: center;
    }

    .qgg-table td {
        border-collapse: collapse;
        border-right: 1px solid #c6c6c6 !important;
        border-bottom: 1px solid #c6c6c6 !important;
        padding: 5px 9px;
        font-size: 12px;
        font-weight: normal;
        text-align: center;
        word-break: break-all;
    }

    .qgg-table tr:nth-child(odd) {
        background-color: #fff !important;
    }

    .qgg-table tr:nth-child(even) {
        background-color: #f8f8f8 !important;
    }
</style>

<table class="qgg-table" style="margin: 50px auto">
    <tr>
        <th>检测项</th>
        <th>状态</th>
    </tr>
    <?php foreach ($powerData as $dir => $status) { ?>
        <tr>
            <td><?php echo $dir ?></td>
            <td><?php echo $status ? "读写权限开启" : "读写权限未开启" ?></td>
        </tr>
    <?php } ?>

    <tr>
        <td colspan="2" style="background: #efefef;text-align: right;color: red">
            "未开启"状态会影响网站的正常使用,一定要确保以上文件夹的权限都是"开启"状态
        </td>
    </tr>


    <?php foreach ($existsData as $dir => $status) { ?>
        <tr>
            <td><?php echo $dir ?></td>
            <td><?php echo $status ? "文件存在" : "文件缺失" ?></td>
        </tr>
    <?php } ?>

    <tr>
        <td colspan="2" style="background: #efefef;text-align: right;color: red">
            "文件缺失"会导致网站出现500报错,请补全文件!(.env文件在阿里云虚拟主机上可能无法检测到)
        </td>
    </tr>

    <?php foreach ($sizeData as $dir => $status) { ?>
        <tr>
            <td><?php echo $dir ?></td>
            <td><?php echo sprintf('%.4f M', $status / 1024 / 1024) ?></td>
        </tr>
    <?php } ?>

    <tr>
        <td colspan="2" style="background: #efefef;text-align: right;color: red">
            如项目占用空间较大,请手动删除或优化对应文件夹中的文件(如果数据缓存占用空间较大,建议覆盖版本>=2021.08.2的更新包,可一劳永逸!)
        </td>
    </tr>

    <?php if (count($bomFiles) > 0) { ?>
        <?php foreach ($bomFiles as $file) { ?>
            <tr>
                <td><?php echo $file ?></td>
                <td><?php echo $auto == 1 ? '已自动修改编码' : '请手动修改文件编码' ?></td>
            </tr>
        <?php } ?>

        <tr>
            <td colspan="2" style="background: #efefef;text-align: right;color: red">
                带"bom"的文件会导致网站无法正常使用,请完善文件编码!
            </td>
        </tr>
    <?php } ?>
</table>
